set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
source $VIMRUNTIME/PerlSupport.vim

"Temporarily override PerlSupport
source $VIMRUNTIME/vimrc_Home_FR.vim
source $VIMRUNTIME/nerd.vim
behave mswin

set background=dark
let mapleader = ","
let localleader = ","

"TODO: Figure out how to set the default dictioray without having to use spell
"set dictionary "F:\Perl\eg\PerlEx\words"

"Remember to reload a session do: vim -S Session.vim
set sessionoptions=resize,winsize,winpos,blank,buffers,curdir,folds,help,options,tabpages

set diffexpr=MyDiff()

au BufAdd,BufNewFile,BufRead * nested tab sball

" auto-change directory to current buffer
autocmd BufEnter * :cd %:p:h

" Auto detects file type
let g:fencview_autodetect = 1


function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let eq = ''
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      let cmd = '""' . $VIMRUNTIME . '\diff"'
      let eq = '"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction


fu! ZOPinit()
    if exists("$ZOPDIRS")
        let files = globpath($ZOPDIRS, "**/*")
        let s:fileList = split(files)
    endif
endfu

"call ZOPinit()

fu! LOCATEIT(param)
    "treat as a pass through to locate
    let l:index = 1
    let l:numberedMatches = []
    let l:locateinput = substitute(input(""), "^\\s\*", "", "")

	if strlen(l:locateinput)
		if a:param
			let l:expressions = split(system('locate '.a:param.' '.l:locateinput), '\n')
		else
			let l:expressions = split(system('locate '.l:locateinput), '\n')
		endif
		for l:expression in l:expressions
			call add(l:numberedMatches, l:index . ". ". l:expression)
			let l:index = l:index + 1
		endfor
		if len(l:numberedMatches) > 0
			let l:index = inputlist(l:numberedMatches)
			if l:index > 0 && l:index <= len(l:numberedMatches)
				let l:index = l:index - 1
				execute "tabnew " . substitute(l:numberedMatches[l:index], "\\(\\d\\+\. \\)","", "")
			endif
		endif
	endif
endfu


fu! ZOP()
    let index = 1
    let matches = []
    let numberedMatches = []
    let expressions = split(input(""))
    if len(expressions) > 0
        for filePath in s:fileList
            let doContinue = 0
            for expression in expressions
                if filePath !~? expression
                    let doContinue = 1
                    break
                endif
            endfor
            if doContinue == 0
                call add(matches, filePath)
                call add(numberedMatches, index . ". ". filePath)
                let index = index + 1
            endif
        endfor
        if len(matches) > 0
            let index = inputlist(numberedMatches)
            if index > 0 && index <= len(matches)
                let index = index - 1
                execute "e " . matches[index]
            endif
        endif
    endif
endfu
map <leader>op :call LOCATEIT('-d "C:\Documents and Settings\ddarcy\Application Data\Locate32\files.dbs"')<enter>

if has("unix")
	map <leader>e :e <C-R>=expand("%:p:h) . "/"<CR>
else
	map <leader>e :e <C-R>=expand("%:p:h") . "\\"<CR>
endif

map <leader>cd :cd %:p:h<CR>
